import {approval, findAllByPaging, findTotal, removeById, add, findLeavesByUser, 
          joinLeave, searchLeaveByPage, deleteLeaveById, updateLeaveInfo} from '@/api/leave'

const state = {
  leaves: []
};

const mutations = {
  SET_LEAVES: (state, leaves) => {
    state.leaves = leaves
  }
};
const actions = {
  findAllByPaging({commit}, params) {
    return new Promise((resolve, reject) => {
      findAllByPaging(params).then(response => {
        commit('SET_LEAVES', response.data);
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  //查询总记录条数
  findTotal({commit}) {
    return new Promise((resolve, reject) => {
      findTotal().then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  //管理员根据ID删除活动信息
  removeById({commit}, leaveId) {
    return new Promise((resolve, reject) => {
      removeById(leaveId).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 管理员根据ID删除活动信息
  approval({commit}, data) {
    return new Promise((resolve, reject) => {
      approval(data).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
// 发布活动、招新
  add({commit}, data) {
    return new Promise((resolve, reject) => {
      add(data).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 根据用户查询活动信息
  findLeavesByUser({commit}, params) {
    return new Promise((resolve, reject) => {
      findlLeavesByUser(params).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 用户报名参加活动
  joinLeave({commit}, data) {
    return new Promise((resolve, reject) => {
      joinLeave(data).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  //荍：分页查询自己的所有请假列表
      searchLeaveByPage({commit}, params) {
      return new Promise((resolve, reject) => {
          searchLeaveByPage(params).then(response => {
          commit('SET_LEAVES', response.data);
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
  
    //荍：删除自己的请假记录
    deleteLeaveById({commit}, deleteId) {
      return new Promise((resolve, reject) => {
          deleteLeaveById(deleteId).then(response => {
          commit('SET_LEAVES', response.data);
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
  
      //荍：更新自己的请假记录
    updateLeaveInfo({commit}, params) {
      return new Promise((resolve, reject) => {
        updateLeaveInfo(params).then(response => {
          commit('SET_LEAVES', response.data);
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

